﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Globalization;
using System.Security.Cryptography;
using System.Text;
using System.Web;

namespace VkApiVer2.extensions
{
    public static class MyExtensions
    {
        public static string GetMD5Hash(string text)
        {
            var x = new MD5CryptoServiceProvider();
            byte[] bs = Encoding.UTF8.GetBytes(text);
            bs = x.ComputeHash(bs);
            var s = new StringBuilder();
            foreach(var b in bs)
            {
                s.Append(b.ToString("x2").ToLower());
            }
            return s.ToString();
        }

        public static String ConstructQueryString(NameValueCollection parameters)
        {
            var items = new List<string>();

            foreach(String name in parameters)
                items.Add(String.Concat(name, "=", HttpUtility.UrlEncode(parameters[name])));

            return String.Join("&", items.ToArray());
        }

        public static bool IsSet(this Enum input, Enum matchTo)
        {
            return (Convert.ToInt32(input) & Convert.ToInt32(matchTo)) != 0;
        }

        public static string ValueToString(this Enum input)
        {
            return input.ToString();
        }
    }
}
